//
//  GameController.m
//  Tables
//
//  Created by mathias Muller on 8/31/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "GameController.h"

#import "NumberDataSource.h"


@implementation GameController
@synthesize result, operationLabel, okButton, textResult, lengthControl, previousColor, errorLabel;
@synthesize newChallenge;

- (void)dealloc {
	[result release];
	[operationLabel release];
	[okButton release];
	[textResult release];
	[lengthControl release];
	[previousColor release];
	[errorLabel release];

	[super dealloc];
}


- (void)awakeFromNib {
}

- (NSInteger) getNbDigit:(NSInteger) num {
	NSInteger digit = 0;
	do {
		digit++;
	}while ((num = num/10) > 0);
	return digit;
}

- (void)init:(Challenge*)nc {
	self.newChallenge = nc;
	NumberDataSource *ds = [[NumberDataSource alloc] initWithLength:[self getNbDigit:[nc getMaxResponseValue]]];
	[ds setTextResult:textResult];
	[ds reset];
	result.delegate = ds;
	result.showsSelectionIndicator = TRUE;
	lengthControl.progress = 0.0;
	[self displayQuestion];
	[result becomeFirstResponder];
}

- (void) displayQuestion {
	for (int i=0; i< ((NumberDataSource*)result.delegate).length;i++) {
			[result selectRow:0 inComponent:i animated:TRUE];
	}
	[(NumberDataSource*)result.delegate reset];
	NSString * currentOperation = [self.newChallenge.currentOperation toString];
	self.operationLabel.text = currentOperation;
	lengthControl.progress = lengthControl.progress + 1.0/newChallenge.conf.challengeLength;
}

- (void) displayError:(Operation *) o {
	self.previousColor = [self.okButton titleColorForState:UIControlStateNormal];
	self.errorLabel.hidden = FALSE;
	self.errorLabel.text = [NSString stringWithFormat:@"Good response was: %d",[o getResult]];

	[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(updateButtonLabel)  userInfo:nil repeats:FALSE];
}

- (void) updateButtonLabel {
	self.errorLabel.hidden = TRUE;
	if(![self.newChallenge hasNextOperation]) {
		[self.okButton setTitle:@"Finish" forState:UIControlStateNormal];
	}else {
		[self.okButton setTitle:@"Next" forState:UIControlStateNormal];
	}
}

- (NSInteger) getEnteredValue {
	return [self.textResult.text integerValue];
	//return [result.delegate getSelectedValue];
}


-(IBAction)okPressed:(id)sender {
	self.newChallenge.currentOperation.result =  [self getEnteredValue];
	if(![self.newChallenge.currentOperation isCorrect]) {
		[self displayError:self.newChallenge.currentOperation];
	}
	
	if([self.newChallenge hasNextOperation]) {
		[self.newChallenge nextOperation];
		[self displayQuestion];
	} else {
		[[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName:@"Game_Ended" object:self.newChallenge]];
		//switch view
		//[navigationController pushViewController:resultController animated:YES];
	}
}
- (void)cancelGame {
	[self.textResult resignFirstResponder];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	NSLog(@"event %@",event);
	if([[event touchesForView:self.view] count] >0) {
		[self.textResult resignFirstResponder];
	}
}

@end
